const {query} = require('../models/db');

let unameCache = null;
module.exports.initLocals = async (req, res, next) => {
    // 全局数据可放至res.locals中
    if (unameCache) {
        res.locals.uname = unameCache;
        next();
    } else {
        const sql = 'SELECT * FROM routes;';
        try {
            const Unames = await query(sql);      
            unameCache = res.locals.uname = Unames;
            next();
        } catch (e) {
            next(e);
        }
    }
};
